package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

@Service
@Transactional
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private IdWorker idWorker;
    /**
     * 添加
     */
    public void saveArticle(Article article) {
        article.setId(String.valueOf(idWorker.nextId()));
        articleDao.save(article);
    }

    /**
     * 文章搜索
     */
    public Page<Article> searchArticle(String title, String content, Integer page, Integer size) {
        Pageable of = PageRequest.of(page - 1, size);
        Page<Article> articles = articleDao.findByTitleOrContentLike(title, content, of);
        return articles;
    }
}
